﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;

namespace Hp.Vertica.Migration.SqlServer
{
    public abstract class ViewBase : TableBase
    {
        public String Definition
        {
            get
            {
                using (SqlConnection conn = new SqlConnection(this.Database.DataSource.ConnectionString))
                {
                    conn.Open();
                    var cmd = conn.CreateCommand();
                    cmd.CommandText = @"SELECT m.definition 
                                    FROM sys.all_views v 
                                    JOIN sys.all_sql_modules m 
                                    ON v.object_id = m.object_id
                                    JOIN sys.schemas s
                                    ON v.schema_id = s.schema_id
                                    WHERE v.name = @ViewName
                                    AND s.name = @SchemaName";
                    cmd.Parameters.Add(new SqlParameter("ViewName", this.Name));
                    cmd.Parameters.Add(new SqlParameter("SchemaName", this.Schema.Name));
                    return cmd.ExecuteScalar().ToString();
                }
            }
        }
    }
}
